using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class UyNhiemChiDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
        public ResultObj SaveUyNhiemChi(UyNhiemChi uynhiemchi)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddUyNhiemChi", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddUyNhiemChi";
			SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, uynhiemchi.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, uynhiemchi.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, uynhiemchi.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, uynhiemchi.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, uynhiemchi.History);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, uynhiemchi.NgayThang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IdNganHang", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdNganHang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IdTaiKhoanGiaoDich", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdTaiKhoanGiaoDich);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdNhaCungCap", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdNhaCungCap);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdNganHangNhan", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdNganHangNhan);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdTaiKhoanNhan", DbType.Int32, ParameterDirection.Input, uynhiemchi.IdTaiKhoanNhan);
            cmd.Parameters.Add(param);
            param = CreateParameter("@NoiDung", DbType.String, ParameterDirection.Input, uynhiemchi.NoiDung);
			cmd.Parameters.Add(param);
            param = CreateParameter("@SoTien", DbType.Int64, ParameterDirection.Input, uynhiemchi.SoTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@NguoiLap", DbType.String, ParameterDirection.Input, uynhiemchi.NguoiLap);
			cmd.Parameters.Add(param);
            param = CreateParameter("@GhiChu", DbType.String, ParameterDirection.Input, uynhiemchi.GhiChu);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, uynhiemchi.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}

        // Get all UyNhiemChi in DB
        public ResultObj GetAllUyNhiemChi()
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
            List<UyNhiemChi> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllUyNhiemChi", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
                lst = new List<UyNhiemChi>();
                UyNhiemChi item = null;
			    // reader all data from DB one by one
			    while (reader.Read())
			    {
                    item = new UyNhiemChi();
			        item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.IdNganHang = Convert.ToInt32(reader["IdNganHang"]);
                    item.IdTaiKhoanGiaoDich = Convert.ToInt32(reader["IdTaiKhoanGiaoDich"]);
                    item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
                    item.IdNganHangNhan = Convert.ToInt32(reader["IdNganHangNhan"]);
                    item.IdTaiKhoanNhan = Convert.ToInt32(reader["IdTaiKhoanNhan"]);
                    item.NoiDung = Convert.ToString(reader["NoiDung"]);
                    item.SoTien = Convert.ToInt64(reader["SoTien"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.GhiChu = Convert.ToString(reader["GhiChu"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			        lst.Add(item);
			    }
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Get all UyNhiemChi in DB with condition
        public ResultObj GetAllUyNhiemChiByID(UyNhiemChi uynhiemchi)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
            List<UyNhiemChi> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllUyNhiemChiByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, uynhiemchi.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
                lst = new List<UyNhiemChi>();
                UyNhiemChi item = null;
			    // reader all data from DB one by one
			    while (reader.Read())
			    {
                    item = new UyNhiemChi();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.IdNganHang = Convert.ToInt32(reader["IdNganHang"]);
                    item.IdTaiKhoanGiaoDich = Convert.ToInt32(reader["IdTaiKhoanGiaoDich"]);
                    item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
                    item.IdNganHangNhan = Convert.ToInt32(reader["IdNganHangNhan"]);
                    item.IdTaiKhoanNhan = Convert.ToInt32(reader["IdTaiKhoanNhan"]);
                    item.NoiDung = Convert.ToString(reader["NoiDung"]);
                    item.SoTien = Convert.ToInt64(reader["SoTien"]);
                    item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
                    item.GhiChu = Convert.ToString(reader["GhiChu"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			        lst.Add(item);
			    }
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Delete a UyNhiemChi out of DB with condition
        public ResultObj DeleteUyNhiemChi(UyNhiemChi uynhiemchi)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteUyNhiemChi", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteUyNhiemChi";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, uynhiemchi.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }
	}
}
